<?php

namespace Paypy;

use think\Log;


class PaypyApiClient
{
    private $api;

    function __construct()
    {
        $this->api = new PaypyApi();
    }

    /**
     * 支付接口
     * @param $amount
     * @param $userId
     * @param $orderSn
     * @return mixed
     */
    public function payment($amount, $userId, $orderSn)
    {
        $apiPath = PaypyApiConstent::API_PATH_PAY;
        $data = [
            'return_type' => 'pc',
            'pay_id' => PaypyApiConstent::PAY_ID_QRCODE,
            'amount' => $amount,
            'success_url' => urlencode('https://m.idramabox.com/h5/pages/vipcard/paymentStatus?status=1'),
            'error_url' => urlencode('https://m.idramabox.com/h5/pages/vipcard/paymentStatus?status=0'),
            'out_uid' => $userId,
            'out_trade_no' => $orderSn,
            'version' => "v2.1",
            'client_ip' => "",
            'sign_type' => "md5",
            'country' => PaypyApiConstent::COUNTRY_MARK_PHP
        ];
        Log::debug('paypy payment,params:' . json_encode($data));
        return $this->api->post($apiPath, $data);
    }

    /**
     * 验证回调签名
     * @param $params
     * @return false
     */
    public function checkCallbackSign($params)
    {
        if (empty($params) || empty($params['sign'])) {
            return false;
        }
        $sign = $this->api->callbackSign($params);
        Log::info('callback params:' . json_encode($params) . 'sign:' . $sign);
        return ($params['sign'] == $sign);
    }

    /**
     * 验证订单接口
     * @param $amount
     * @param $userId
     * @param $orderSn
     * @return mixed
     */
    public function check($amount, $orderSn)
    {
        $apiPath = PaypyApiConstent::API_PATH_CHECK;
        $data = [
            'amount' => $amount,
            'out_trade_no' => $orderSn,
        ];
        Log::debug('paypy check,params:' . json_encode($data));
        return $this->api->post($apiPath, $data);
    }
}